package org.roger.index.filter;

import java.io.IOException;
import java.util.LinkedList;

import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.util.AttributeSource;

/**
 * 句号分割过滤器
 * 例如：cat.your 会分割成两个词：cat和your
 * @author luoweiyi
 */
public class SplitSpotFilter extends TokenFilter{

	private CharTermAttribute termAtt;
	private PositionIncrementAttribute posIncrAtt;
	private OffsetAttribute offSetAtt;
	
	private AttributeSource.State current;
	//存放分割词集合
	private LinkedList<String> words = new LinkedList<String>();
	
	private int offset = 0;
	
	public SplitSpotFilter(TokenStream input) {
		super(input);
		termAtt = addAttribute(CharTermAttribute.class);
		posIncrAtt = addAttribute(PositionIncrementAttribute.class);
		offSetAtt = addAttribute(OffsetAttribute.class);
	}

	@Override
	public boolean incrementToken() throws IOException {
		try {
			
			if(!words.isEmpty()){
				String word = words.poll();
				restoreState(current);
				if(word != null){
					termAtt.setEmpty();
					termAtt.append(word);
					offSetAtt.setOffset(offSetAtt.startOffset()+offset, offSetAtt.startOffset()+word.length()+offset);
					posIncrAtt.setPositionIncrement(posIncrAtt.getPositionIncrement());
					offset+=word.length()+1;//加上之前一个词的偏移，还要加上句号的偏移
					current = captureState();
					return true;
				}
			}
			
			if(!input.incrementToken()){
				return false;
			}
			
			String encoded = termAtt.toString();
			
			if(encoded.contains(".")){
				String per = encoded.split("\\.")[0];
				String suf = encoded.split("\\.")[1];
				words.add(per);
				words.add(suf);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return true;
	}
}
